<template>
	<view class="classlist">
		<view @click="safeBack">我的下载</view>
		<view class="content">
			<view class="item" v-for="item in downLoadPicurlList" :key="item.id">
			  <navigator :url="'/pages/perview/perview?id=' + item.id" open-type="reLaunch">
			    <image :src="item.smallPicurl" mode="aspectFill"></image>
			  </navigator>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {ref} from "vue";
	import { onLoad } from '@dcloudio/uni-app'; // ✅ 正确来源
	import{ apiGetUserInfo_ctt} from "@/api/api_ctt.js"
	const downLoadPicurlList = ref([])
	const userInfo = ref([])
	
	function safeBack() {
	 
	    uni.switchTab({ url: '/pages/user/user' }); // 或者换成你的首页路径
	  
	}
	 
	const getUserInfo = async () => {
	  const res = await apiGetUserInfo_ctt();
	  const rawList = res.data[0].downloadSize || [];
	
	  // 使用 Set 去重
	  const uniqueList = [];
	  const seen = new Set();
	
	  for (const item of rawList) {
	    const key = `${item.classid}_${item.id}`;
	    if (!seen.has(key)) {
	      seen.add(key);
	      uniqueList.push(item);
	    }
	  }
	
	  console.log("去重后的列表:", uniqueList);
	
	  // 清空旧列表
	  downLoadPicurlList.value = [];
	
	  // 逐条请求
	  for (const item of uniqueList) {
	    await fetchWallItem(item.classid, item.id);
	  }
	};

	const fetchWallItem = async (classid, id) => {
	  try {
	    const res = await uni.request({
	      url: 'https://xrqr0h0x1w.gzg.sealos.run/get_downLoadInfo', // 替换成你的真实地址
	      method: 'POST',
	      data: { classid, id },
	      header: { 'Content-Type': 'application/json' }
	    })
	
	    if (res.data.code === 200 && res.data.data) {
	      const { _id, smallPicurl } = res.data.data
	      downLoadPicurlList.value.push({ 
	        id: _id, 
	        smallPicurl, 
	        debugClassid: classid,
	        debugId: id 
	      })

		  console.log("获成功" ,downLoadPicurlList.value)
	    } else {
	      console.warn('获取失败:', res.data.message)
	    }
	  } catch (err) {
	    console.error('单个请求失败:', err)
	  }
	}
	 getUserInfo()
</script>

<style scoped lang="scss">
	.classlist{
		.content{
			display: grid;
			grid-template-columns: repeat(3,1fr);
			gap:5rpx;
			padding:5rpx;
			 .item{
			 				navigator{
			 					image{
			 						width: 100%;
			 						height: 300rpx; /* 设置一个合理的高度 */
			 						object-fit: cover;
			 					}
			 				}
			 			}
			
		}
	}
</style>
